package com.ikingtech.platform.business.workbench.controller;

import com.ikingtech.framework.sdk.context.exception.FrameworkException;
import com.ikingtech.framework.sdk.core.response.R;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.framework.sdk.web.annotation.ApiController;
import com.ikingtech.framework.sdk.workbench.api.ScheduleApi;
import com.ikingtech.framework.sdk.workbench.model.ScheduleDTO;
import com.ikingtech.framework.sdk.workbench.model.ScheduleQueryParamDTO;
import com.ikingtech.platform.business.workbench.entity.ScheduleDO;
import com.ikingtech.platform.business.workbench.service.ScheduleService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tie yan
 */
@RequiredArgsConstructor
@ApiController(value = "/schedule", name = "工作台-日程管理", description = "工作台-日程管理")
public class ScheduleController implements ScheduleApi {

    private final ScheduleService service;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<String> add(ScheduleDTO schedule) {
        ScheduleDO entity = Tools.Bean.copy(schedule, ScheduleDO.class);
        entity.setId(Tools.Id.uuid());
        this.service.save(entity);
        return R.ok(entity.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Object> delete(String id) {
        this.service.removeById(id);
        return R.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Boolean> update(ScheduleDTO schedule) {
        if (!this.service.exist(schedule.getId())) {
            throw new FrameworkException("对象不存在");
        }
        ScheduleDO entity = Tools.Bean.copy(schedule, ScheduleDO.class);
        this.service.updateById(entity);
        return R.ok();
    }

    @Override
    public R<ScheduleDTO> detail(String id) {
        ScheduleDO entity = this.service.getById(id);
        if (null == entity) {
            throw new FrameworkException("对象不存在");
        }
        ScheduleDTO schedule = Tools.Bean.copy(entity, ScheduleDTO.class);
        return R.ok(schedule);
    }

    @Override
    public R<List<ScheduleDTO>> page(ScheduleQueryParamDTO queryParam) {
        return R.ok(this.service.listPage(queryParam).convert(entity -> Tools.Bean.copy(entity, ScheduleDTO.class)));
    }

    @Override
    public R<List<ScheduleDTO>> all() {
        return R.ok(Tools.Coll.convertList(this.service.list(), entity -> Tools.Bean.copy(entity, ScheduleDTO.class)));
    }

    @Override
    public R<Map<String, Integer>> countByDate(ScheduleQueryParamDTO queryParam) {
        List<ScheduleDO> entities = this.service.listByEstimateStartTimeBetween(queryParam.getStartTime(), queryParam.getEndTime());
        Map<String, Integer> result = new HashMap<>();
        entities.forEach(entity -> {
            String dateStr = Tools.DateTime.Formatter.simpleDate(entity.getEstimateStartTime().toLocalDate());
            Integer count = result.computeIfAbsent(dateStr, k -> 0);
            result.put(dateStr, count + 1);
        });
        return R.ok(result);
    }
}